package com.yami.shop.personnft.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.ExcelUtils;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.personnft.common.model.PersonNft;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.personnft.common.vo.NftPersonVO;
import com.yami.shop.personnft.common.vo.PersonNftStatisticsVo;
import com.yami.shop.security.common.util.AuthUserContext;
import com.yami.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * spu信息
 *
 * @author FrozenWatermelon
 * @date 2020-10-28 15:27:24
 */
@RestController("adminPersonnftController")
@RequestMapping("/admin/personnft")
@Tag(name = "admin-personnft信息")
@Slf4j
public class PersonnftController {
    @Autowired
    private PersonNftService personNftService;
    @Autowired
    private UserService userService;

    /**
     * 获取个人持仓目分类中具体token列表
     *
     * @param personNft
     * @return list->tokenId shortUri
     */
    @GetMapping("/getOneClassList")
    @Operation(summary = "获取平台信息列表", description = "分页获取平台信息列表")
    public ServerResponseEntity<IPage<PersonNft>> getOneClassList(PageParam pageDTO, PersonNft personNft) {
        if (personNft.getUserId() == null) {
            throw new YamiShopBindException("yami.sys.illegalArgument");
        }
        if (personNft.getProductId() == null) {
            throw new YamiShopBindException("yami.sys.illegalArgument");
        }
        personNft.setStatus(0);
        IPage<PersonNft> page = personNftService.page(pageDTO, personNft);
        return ServerResponseEntity.success(page);
    }


//    /**
//     * 获取个人持仓目分类属性信息
//     * @param userId
//     * @return ,denomId, dawName,dawUriType,shopName
//     */
//    @GetMapping("/getOneClass")
//    @Operation(summary =  "获取平台信息列表", description = "分页获取平台信息列表")
//    public ServerResponseEntity<IPage<Map<String, Object>>> getOneClassByUserId(PageParam pageDTO, String userId) {
//    	if(userId==null){
//            throw new YamiShopBindException("yami.sys.illegalArgument");
//    	}
//        return ServerResponseEntity.success(personNftService.getOneClassByUserId(pageDTO,userId));
//    }

    @GetMapping("/getAllList")
    @Operation(summary = "获取平台信息列表", description = "分页获取平台信息列表")
    public ServerResponseEntity<IPage<PersonNft>> getAllList(PageParam pageDTO, PersonNft personNft) {
        personNft.setStatus(0);
        personNft.setDawProductCode("KT");
        IPage<PersonNft> page = personNftService.page(pageDTO, personNft);
        return ServerResponseEntity.success(page);
    }


    /**
     * 获取会员持仓情况
     *
     * @param pageDTO
     * @param user
     * @return
     */
    @GetMapping("/statistics/page")
    @Operation(summary = "获取会员持仓情况", description = "获取会员持仓情况")
    public ServerResponseEntity<PageVO<PersonNftStatisticsVo>> getStatistics(PageDTO pageDTO, User user) {
        PageVO<PersonNftStatisticsVo> pageVO = personNftService.getStatistics(pageDTO, user);
        return ServerResponseEntity.success(pageVO);
    }


    /**
     * 个人持仓列表
     *
     * @param pageDTO
     * @param nftPersonVO
     * @return
     */
    @GetMapping("/positionPage/byUserId")
    @Operation(summary = "获取会员持仓情况", description = "获取会员持仓情况")
    public ServerResponseEntity<PageVO<NftPersonVO>> getPositionByUserId(PageDTO pageDTO, NftPersonVO nftPersonVO) {
        PageVO<NftPersonVO> pageVO = personNftService.getPositionByUserId(pageDTO, nftPersonVO);
        return ServerResponseEntity.success(pageVO);
    }


    /**
     * 导入
     */
    @RequestMapping("inExeclPersonNft")
    public ServerResponseEntity<Map<String, Object>> inExeclPersonNft(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        List<User> list = ExcelUtils.importExcel(file, 0, 1, User.class);
        if (list == null || list.size() == 0) {
            throw new YamiShopBindException("yami.file.error");
        }
        User tem = list.get(0);
        if (tem.getUserMobile() == null && tem.getUserId() == null) {
            throw new YamiShopBindException("yami.file.error");
        }
        String res = ""; // 返回失败原因
        int insertCount = 0; // 导入成功
        int errorCount = 0; // 导入失败数
        List<User> list2 = new ArrayList<>();
        for (User userDTO : list) {
            try {
                if (userDTO.getUserMobile() == null && userDTO.getUserId() == null) {
                    throw new YamiShopBindException("yami.file.error");
                }
                User byPhoneNum = null;
                if (userDTO.getUserId() != null) {
                    byPhoneNum = userService.getUserByUserId(userDTO.getUserId());
                }
//                else{
//                    byPhoneNum = userService.getByPhoneNum(userDTO.getUserMobile());
//                }
                if (byPhoneNum == null) {
                    throw new YamiShopBindException("yami.phone.number.not.exists");
                }
                if (byPhoneNum.getStatus() != 1) {
                    throw new YamiShopBindException("yami.personnft.user.status.error");
                }
                if (StringUtils.isEmpty(byPhoneNum.getIdNumber())) {
                    throw new YamiShopBindException("yami.webservice.authentication.isNotempty");
                }
                list2.add(byPhoneNum);
                insertCount++;
            } catch (Exception e) {
                res += userDTO.getUserMobile() + "/" + userDTO.getUserId() + ":" + e.getMessage() + "<br/>";
                errorCount++;
            }
        }
        map.put("list", list2);
        map.put("msg", res);
        map.put("insertCount", insertCount);
        map.put("errorCount", errorCount);
        return ServerResponseEntity.success(map);
    }

}
